package com.app.service;

import java.util.List;

import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.dao.UserCarDao;
import com.app.entity.ResidentialWorker;
import com.app.entity.userCar;
import com.app.exception.ValidateException;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;

@Service
public class UserCarService {
    @Autowired
    private UserCarDao userCarDao;

    /**
     * 获取员工列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     * @throws ValidationException
     */
    public ReturnResultResponseVO<userCar> getSuserCarList(int currentPage, int pageSize, String carParkName)
            throws ValidationException {

        ReturnResultResponseVO<userCar> responseData = new ReturnResultResponseVO<userCar>();
        int countPage = 0;
        int count = userCarDao.getCount(carParkName,null);
        try {
            if (count % pageSize == 0) {
                countPage = count / pageSize;
            } else {
                countPage = count / pageSize + 1;
            }
            if (currentPage < 1) {
                currentPage = 1;
            }
            if (countPage == 0) {
                countPage = 1;
            }
            if (currentPage > countPage) {
                currentPage = countPage;
            }
            PageVO pageVO = new PageVO();
            pageVO.setCountPage(countPage);
            pageVO.setCurrentPage(currentPage);
            pageVO.setPagesize(pageSize);
            responseData.setPage(pageVO);
            List<userCar> userCars = userCarDao.queryUserCarList((currentPage - 1) * pageSize, pageSize, carParkName,null);
            responseData.setData(userCars);
        } catch (Exception e) {
            throw new ValidateException("获取用停车列表异常");
        }
        return responseData;
    }
    public ReturnResultResponseVO<userCar> getSuserCarPageList(int currentPage, int pageSize, String userName)
    		throws ValidationException {
    	
    	ReturnResultResponseVO<userCar> responseData = new ReturnResultResponseVO<userCar>();
    	int countPage = 0;
    	int count = userCarDao.getCount(null,userName);
    	try {
    		if (count % pageSize == 0) {
    			countPage = count / pageSize;
    		} else {
    			countPage = count / pageSize + 1;
    		}
    		if (currentPage < 1) {
    			currentPage = 1;
    		}
    		if (countPage == 0) {
    			countPage = 1;
    		}
    		if (currentPage > countPage) {
    			currentPage = countPage;
    		}
    		PageVO pageVO = new PageVO();
    		pageVO.setCountPage(countPage);
    		pageVO.setCurrentPage(currentPage);
    		pageVO.setPagesize(count);
    		responseData.setPage(pageVO);
    		List<userCar> userCars = userCarDao.queryUserCarList((currentPage - 1) * pageSize, pageSize, null, userName);
    		responseData.setData(userCars);
    	} catch (Exception e) {
    		throw new ValidateException("获取用停车列表异常");
    	}
    	return responseData;
    }

    /**
     * 获取车位详情
     *
     * @param userCarId
     * @return
     */
    public userCar userCarDetail(int userCarId) {

        userCar userCar = new userCar();
        try {
            userCar = userCarDao.getUserCarDetail(userCarId);
        } catch (Exception e) {
            throw new ValidateException("获取用员工列表异常");
        }
        return userCar;
    }

    /**
     * 更新车位信息
     *
     * @param userCar
     */
    public void updateCar(userCar userCar) {
        try {
            userCarDao.updateUserCar(userCar);
        } catch (Exception e) {
            throw new ValidateException("更新用员工异常");
        }
    }

    /**
     * 新增车位信息
     *
     * @param userCar
     */
    public void addUserCar(userCar userCar) {
        try {
            userCarDao.addUserCar(userCar);
        } catch (Exception e) {
            throw new ValidateException("新增车位信息异常");
        }
    }

    /**
     * 删除车位信息
     *
     * @param userCarId
     */
    public void deleteUserCar(int userCarId) {
        try {
            userCarDao.deleteUserCar(userCarId);
        } catch (Exception e) {
            throw new ValidateException("删除车位信息异常");
        }
    }
}
